import pygame
from settings_manager import *
from pygame.rect import Rect

from level_resources import LevelResources
from animation import Animation
from pygame.sprite import  LayeredUpdates

from bomb import Bomb

tiles = 15

class Player:
	def __init__(self, screen, pType, x, y, group, pNr):
		self.group = group
		self.screen = screen
		self.pType = pType

		self.width, self.height = SettingsManager.settings.resolution[0] / tiles, SettingsManager.settings.resolution[0] / tiles
		self.rect = Rect(0, 0, self.width, self.height)
		self.alive = True
		self.speed = 7 * SettingsManager.settings.resolution[0] / SettingsConst.Medium[0]
		self.lastTime = pygame.time.get_ticks()
		self.bombs = []
		self.life = 2
		self.points = 0

		self.auto = False
		self.fromCell = ()
		self.toCell = ()

		self.pNr = pNr

		self.setPosition(x, y)

		self.idle = Animation(screen, self.rect, 
			LevelResources.pImages[pType]["down"], 
			LevelResources.pDelays, True, 0)

		self.up = Animation(screen, self.rect, 
			LevelResources.pImages[pType]["up"], 
			LevelResources.pDelays, True, 0)

		self.down = Animation(screen, self.rect, 
			LevelResources.pImages[pType]["down"], 
			LevelResources.pDelays, True, 0)

		self.left = Animation(screen, self.rect, 
			LevelResources.pImages[pType]["left"], 
			LevelResources.pDelays, True, 0)

		self.right = Animation(screen, self.rect, 
			LevelResources.pImages[pType]["right"], 
			LevelResources.pDelays, True, 0)

		self.anim = None
		self.setAnim(self.idle)

	def setPosition(self, x, y):
		self.rect.x = x
		self.rect.y = y

	def setObPl(self, obstacles, players):
		self.obstacles = obstacles
		Bomb.setObstacles(obstacles)
		Bomb.setPlayers(players)

	def getPosCentered(self):
		return int((self.rect.x + self.width / 2) / self.width), int((self.rect.y + self.height / 2) / self.height)
		
	def setAnim(self, anim):
		if self.anim == anim:
			return None

		if self.anim:
			self.anim.stopAnimation()

		self.anim = anim
		self.group.add(self.anim)
		self.anim.startAnimation()

	def setEnemies(self, enemies):
		self.enemies = enemies

	def hit(self, damage):
		self.life -= damage
		if self.life <= 0:
			self.killPlayer()

	def killPlayer(self):
		self.alive = False
		self.anim.kill()

	def plantBomb(self):
		x = int((self.rect.x + self.rect.width / 2) / self.rect.width) * self.rect.width
		y = int((self.rect.y + self.rect.height / 2) / self.rect.height) * self.rect.height

		if len(self.bombs) and not self.bombs[len(self.bombs) -1].planted:
			bomb = self.bombs[len(self.bombs) - 1]
			bomb.plant(x, y)

	def moveFromCellToCell(self):
		if self.fromCell[1] + 1 == self.toCell[1]:	#Down
			if self.rect.y >= self.toCell[1] * self.height - self.speed:	#Finish
				self.auto = False
				self.rect.x = self.toCell[0] * self.width
				self.rect.y = self.toCell[1] * self.height
				return None
			self.move("down")
			#print "down"

		elif self.fromCell[1] - 1 == self.toCell[1]:	#Up
			if self.rect.y <= self.toCell[1] * self.height + self.speed:	#Finish
				self.auto = False
				self.rect.x = self.toCell[0] * self.width
				self.rect.y = self.toCell[1] * self.height
				return None
			self.move("up")
			#print "up"

		elif self.fromCell[0] + 1 == self.toCell[0]:	#Right
			if self.rect.x >= self.toCell[0] * self.width - self.speed:	#Finish
				self.auto = False
				self.rect.x = self.toCell[0] * self.width
				self.rect.y = self.toCell[1] * self.height
				return None
			self.move("right")
			#print "right"

		elif self.fromCell[0] - 1 == self.toCell[0]:	#Left
			if self.rect.x <= self.toCell[0] * self.width + self.speed:	#Finish
				self.auto = False
				self.rect.x = self.toCell[0] * self.width
				self.rect.y = self.toCell[1] * self.height
				return None
			self.move("left")
			#print "left"
		else:
			self.auto = False

	def move(self, direction):
		cError = 10
		if direction == "up":
			if self.rect.y > 0:
				t = True
				x = int(self.rect.x / self.rect.width)
				y = int((self.rect.y - self.speed) / self.rect.height)
				if (x, y) in self.obstacles and not self.obstacles[(x, y)].destroyed and (
					self.obstacles[(x, y)].rect.colliderect(
						Rect(
							self.rect.x + cError, self.rect.y - self.speed, self.rect.width - cError, self.rect.height
							)
						)
					):
					t = False
				if (x + 1, y) in self.obstacles and not self.obstacles[(x + 1, y)].destroyed and (
					self.obstacles[(x + 1, y)].rect.colliderect(
						Rect(
							self.rect.x + cError, self.rect.y - self.speed, self.rect.width - cError, self.rect.height
							)
						)
					):
					t = False

				if t:
					self.rect.y -= self.speed
			self.setAnim(self.up)
		elif direction == "down":
			if self.rect.y < (SettingsManager.settings.resolution[0] - self.height):
				t = True
				x = int(self.rect.x / self.rect.width)
				y = int((self.rect.y + self.speed) / self.rect.height)
				
				if (x, y + 1) in self.obstacles and not self.obstacles[(x, y + 1)].destroyed and (
					self.obstacles[(x, y + 1)].rect.colliderect(
						Rect(
							self.rect.x + cError, self.rect.y, self.rect.width - cError, self.rect.height + self.speed
							)
						)
					):
					t = False
				if (x + 1, y + 1) in self.obstacles and not self.obstacles[(x + 1, y + 1)].destroyed and (
					self.obstacles[(x + 1, y + 1)].rect.colliderect(
						Rect(
							self.rect.x + cError, self.rect.y, self.rect.width - cError, self.rect.height + self.speed
							)
						)
					):
					t = False
				if t:
					self.rect.y += self.speed
			self.setAnim(self.down)
		elif direction == "left":
			if self.rect.x > 0:
				t = True
				x = int((self.rect.x - self.speed) / self.rect.width)
				y = int(self.rect.y / self.rect.height)
				
				if (x, y) in self.obstacles and not self.obstacles[(x, y)].destroyed and (
					self.obstacles[(x, y)].rect.colliderect(
						Rect(
							self.rect.x - self.speed, self.rect.y + cError, self.rect.width, self.rect.height - cError
							)
						)
					):
					t = False
				if (x, y + 1) in self.obstacles and not self.obstacles[(x, y + 1)].destroyed and (
					self.obstacles[(x, y + 1)].rect.colliderect(
						Rect(
							self.rect.x - self.speed, self.rect.y + cError, self.rect.width, self.rect.height - cError
							)
						)
					):
					t = False
				if t:
					self.rect.x -= self.speed
			self.setAnim(self.left)
		elif direction == "right":
			if self.rect.x < (SettingsManager.settings.resolution[0] - self.width):
				t = True
				x = int((self.rect.x + self.speed) / self.rect.width)
				y = int(self.rect.y / self.rect.height)
				
				if (x + 1, y) in self.obstacles and not self.obstacles[(x + 1, y)].destroyed and (
					self.obstacles[(x + 1, y)].rect.colliderect(
						Rect(
							self.rect.x, self.rect.y + cError, self.rect.width + self.speed, self.rect.height - cError
							)
						)
					):
					t = False
				if (x + 1, y + 1) in self.obstacles and not self.obstacles[(x + 1, y + 1)].destroyed and (
					self.obstacles[(x + 1, y + 1)].rect.colliderect(
						Rect(
							self.rect.x, self.rect.y + cError, self.rect.width + self.speed, self.rect.height - cError
							)
						)
					):
					t = False
				if t:
					self.rect.x += self.speed
			self.setAnim(self.right)

		self.lastTime = pygame.time.get_ticks()

	def update(self):
		if not self.alive:
			return None

		#Refresh the list of enemies
		self.enemies = filter(lambda x: x.alive, self.enemies)

		currentTime = pygame.time.get_ticks()
		if currentTime - self.lastTime > 2000:	#Idle for more then 2 secs
			self.setAnim(self.idle)

		#bombs
		if len(self.bombs) == 0:
			self.bombs.append(Bomb(self.screen, self.group, 3, self.pNr))
		elif self.bombs[len(self.bombs) - 1].exploded:
			self.points += self.bombs[len(self.bombs) - 1].points
			del self.bombs[len(self.bombs) - 1]
		
		for bomb in self.bombs:
			bomb.update()
		